<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 小程序公共API服务
 */
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisCity;
use app\ais\model\AisInfoCate;
use app\ais\model\AisInfo;
use app\ais\model\AisInfoFollow;
use app\ais\model\AisInfoMp;
use app\ais\model\AisConfig;
use app\ais\model\AisInfoReply;
use app\ais\model\AisStore;
use app\common\model\SystemUser;
use think\facade\Config;
use util\Util;

class Info extends Base{

    /**
     * 获取配置参数
     * @return void
     */
    public function getconfig(){
        $this->apiSign();
        $result = AisConfig::getConfig($this->miniapp_id);
        if(empty($result)){
            return enjson(204);
        }else{
            $data = json_decode($result->info,true);
            foreach ($data as $key => $value) {
                $data[$key]['money'] = intval($value['money']);
            }
            return enjson(200,$data);
        }        
    }

    /**
     * 读取信息列表
     * @param integer 读取ID
     * @return json
     */
    public function index(){
        $param['page']    = $this->request->param('page/d');
        $param['cate_id'] = $this->request->param('cate_id/d',0);
        $param['citycode']  = $this->request->param('citycode/d');
        $param['state']   = $this->request->param('state/d',1);
        $param['keyword'] = $this->request->param('keyword/s');
        $this->apiSign($param);
        $order = "is_top desc,sort desc,id desc";
        $where = [];
        $where['member_miniapp_id'] = $this->miniapp_id;
        $where['is_lock']           = 0;
        if($param['citycode']){
            $where['citycode']  = AisCity::whereIn('parent_id',AisCity::where(['code' => $param['citycode']])->column('parent_id'))->column('code');
        }
        if($param['cate_id']){
            $where['cate_id']  = $param['cate_id'];
        }else{
            if(empty($param['keyword'])){
                if(!$param['state']){  //关注
                    //读取我的关注UID    
                    $this->isUserAuth();   //我的关注要登录
                    $uid = AisInfoFollow::where(['uid' => $this->user->id])->column('like_uid');
                    if(empty($uid)){
                        return enjson(204,'没有内容');
                    }else{
                        $where['uid'] = $uid;
                    }
                }else{ //热门
                    $order = "is_top desc,sort desc,like desc,id desc";
                }
            }
        }
        $data = AisInfo::apiLists($where,$order,$this->user,$param['keyword']);
        if(empty($data)){
            return enjson(204,'没有内容');
        }else{
            return enjson(200,'成功',$data);
        }
    }

   /**
     * 获取信息评论
     * @param integer 读取ID
     * @return json
     */
    public function reply(){
        $param['page'] = $this->request->param('page/d');
        $param['id']   = $this->request->param('id/d');
        $this->apiSign($param);
        $where['info_id'] = $param['id'];
        $where['state']   = 0;
        $rel = AisInfoReply::with([
            'user' => function($query) {
                $query->field('id,nickname,face');
            },
        ])->where($where)->order('id desc')->paginate(10);
        $data = $rel->toArray()['data'];
        array_walk($data,function(&$item,$key){
            $item['is_like'] = 0;
        });
        if(empty($data)){
            return enjson(204);
        }else{
            return enjson(200,'成功',$data);
        }
    }    

    /**
     * 读取微信API
     * @param integer 读取ID
     * @return json
     */
    public function cate(){
        $this->apiSign();
        $where = [];
        $where['member_miniapp_id'] = $this->miniapp_id;
        $data = AisInfoCate::where($where)->field('id,name')->order('sort desc,id desc')->select()->toArray();
        return enjson(200,'成功',$data);
    }

   /**
     * 获取单个信息
     * @param integer 读取ID
     * @return json
     */
    public function getview(int $info_id){
        $param['info_id'] = $this->request->param('info_id/d',0);
        $this->apiSign($param);
        $rel = AisInfo::with([
            'user' => function($query) {
                $query->field('id,nickname,face');
            },
            'cate' => function($query) {
                $query->field('id,tpl_id,title');
            },
            'mp'   => function($query) {
                $query->field('id,uid,logo,title,is_vip');
            }
        ])->where(['id'=> $info_id])->find();
        if(empty($rel)){
            return enjson(404);
        }
        //模板信息
        if(empty($rel->cate->tpl)){
            $rel['button_name'] = '立即下单';
            $rel['tips']        = '任何有用户发布,下单后请电话再次确认';
            $rel['is_shop']     = 0;
            $rel['users']       = [];
        }else{
            $rel['button_name'] = $rel->cate->tpl->button_name;
            $rel['tips']        = $rel->cate->tpl->tips;
            $rel['is_shop']     = $rel->cate->tpl->is_shop;
            $rel['users']       = json_decode($rel->cate->tpl->users,true);
        }
        $rel['images']      = $rel->images;
        $rel['images_len']  = count($rel->images);
        $rel['fields']      = empty($rel->fields) ? [] : json_decode($rel->fields,true);
        $rel['create_time'] = util::ftime($rel->create_time);
        $rel['is_mp']       = empty($rel->mp)?0:1;
        //是否点赞
        $rel['is_like']     = 0; 
        $rel['like_face']   = empty($rel->like_face) ? [] : json_decode($rel->like_face,true);
        //判断是否被关注
        $rel['is_follow']   = !empty($rel->follow) && !empty($this->user) && $rel->follow->uid = $this->user->id ? 1:0;  
        return enjson(200,'成功',$rel);
    }


    /**
     * 注册城市号
     */
    public function mp(){
        $param['uid'] = $this->request->param('uid/d',0);
        $param['mid'] = $this->request->param('mid/d',0);
        $this->apiSign($param);
        $where = [];
        if($param['mid']){
            $where['id']  = $param['mid'];
        }else{
            $where['uid'] = $param['uid'];
        }
        $rel = AisInfoMp::where($where)->find();
        if($rel){
            $rel->views = AisInfo::where(['uid' => $rel->uid])->count();
            $rel->like  = AisInfoFollow::where(['uid' => $rel->uid])->count();
            $rel->is_follow =  0;
            $rel->vip_time = empty($rel->vip_time)?'0':date('Y-m-d',$rel->vip_time);
            $rel->reg_time = empty($rel->reg_time)?'0':date('Y-m-d',$rel->reg_time);
            if($this->user){
                $rel->is_follow = AisInfoFollow::where(['uid' => $this->user->id,'like_uid'=>$rel->uid])->count();
            }
            //读取好店
            $condition['manage_uid']        = $rel->uid;
            $condition['member_miniapp_id'] = $this->miniapp_id;
            $condition['is_lock']           = 0;
            $info = AisStore::getView($condition);
            if(empty($info)){
                $rel->is_store = 0;
                $rel->store    = [];
            }else{
                $info->distance = getDistance($this->lbs->lng,$this->lbs->lat,$info['longitude'],$info['latitude']);
                $chain = [];
                foreach ($info->chain as $key => $value) {
                    $chain[$key]['title']    = $value->title;
                    $chain[$key]['address']  = $value->address;
                    $chain[$key]['telphone'] = $value->telphone;
                    $chain[$key]['distance'] = getDistance($this->lbs->lng,$this->lbs->lat,$value->longitude,$value->latitude);
                }
                unset($info->chain);
                $info->chain = $chain;
                $rel->store  = $info;
                $rel->is_store = 1;
            }
        }else{
            if($param['uid']){
                $rel['is_store']  = 0;
                $rel['views']     = AisInfo::where(['uid' => $param['uid']])->count();
                $rel['like']      = AisInfoFollow::where(['uid'     => $param['uid']])->count();
                $rel['fans']      = 0;
                $rel['is_follow'] = 0;
                $rel['vip_time']  = 0;
                $rel['reg_time']  = 0;
                $user = SystemUser::where(['id' => $param['uid']])->find();
                $rel['face']      = $user->face;
                $rel['nickname']  = $user->nickname; 
                $rel['logo']      = $user->face; 
                $rel['note']      = '未开通城市号';
                $rel['skin']      = Config::get('upload.upload_relative').'static/ais/skin/2.jpg';
            }
        }
        return enjson(200,$rel);
    }


    /**
     * 读取信息列表
     * @param integer 读取ID
     * @return json
     */
    public function me(){
        $param['page']  = $this->request->param('page/d',1);
        $param['state'] = $this->request->param('state/d',1);
        $param['uid']   = $this->request->param('uid/d',0);
        $param['mid']   = $this->request->param('mid/d',0);
        $param['sign']  = $this->request->param('sign');
        $this->apiSign($param);
        $where = [];
        $where['member_miniapp_id'] = $this->miniapp_id;
        $where['is_lock']           = 0;
        if($param['state']){
            $order = "is_top desc,id desc";
            $where['uid'] = $param['uid'];
        }else{ //热门
            $uid = AisInfoFollow::where(['uid' => $param['uid']])->column('like_uid');
            $where['uid'] = $uid;
            $order = "views desc,sort desc,id desc";
        }
        $data = AisInfo::apiLists($where,$order,$this->user);
        if(empty($data)){
            return enjson(204,'没有内容');
        }else{
            return enjson(200,'成功',$data);
        }
    }
}
